/**
 * 继承后使用，一般用在关卡切换
 */


var itemInfo = cc.Class({
    name: 'itemInfo',
    properties: {
        zIndex: 0,
        scale: 1,
        opacity: 255,
        pos: cc.Vec2.ZERO,
    }
});

cc.Class({
    extends: cc.Component,

    properties: {
        touchNode: cc.Node, //触摸滑动判定区域                      
        content: cc.Node,   //添加item的父节点
        // items: [cc.Node],                                                                                             _4_
        itemInfoGrp: {      //子节点的6个信息，6个点的围 成类圆柱的形状，从正前方第一个节点开始到右边再到后面再到左边               5 /    \3
            default: [],    //                                                                                       6 \ ___/ 2
            type: itemInfo, //                                                                                             1                                             
        },
        moveTime: {
            default: 0.3,
            visible: true,
            displayName: '滑动的时间'
        },
        isCycle: {
            default: true,
            visible: true,
            displayName: '是否循环，能从队尾回到对首'
        },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {
    },



    // update (dt) {},


    /**
     * 
     * @param {*} pageGrp page的数组信息
     * @param {*} pageStartServer page点击或者滑动前调用方法
     * @param {*} pageEndServer page翻页结束后页面信息更新方法
     * @param {*} pageIndex 初始化index
     * @param {*} pageInfoUpdateServer 初始化pageitem 方法
     */
    structPageInit(pageGrp, pageStartServer, pageEndServer, pageIndex, pageInfoUpdateServer) {
        this.cleartweenGrp()
        this.isMoveing = false
        this.tweenGrp = []
        if (pageGrp) {
            this.pageGrp = pageGrp
        }
        if (pageStartServer) {
            this.pageStartServer = pageStartServer
        }
        if (pageEndServer) {
            this.pageEndServer = pageEndServer
        }
        if (pageIndex >= 0) {
            this.pageIndex = pageIndex
        }
        if (pageInfoUpdateServer) {
            this.pageInfoUpdateServer = pageInfoUpdateServer
        }
        this.cardsArray = this.content.children;
        this.nameInit()

        for (let i = 0; i < this.cardsArray.length; i++) {
            this.cardsArray[i].num = i;
            // let initAttr = JSON.parse(this.itemInfoGrp[i]);
            this.cardsArray[i].zIndex = this.itemInfoGrp[i].zIndex;
            this.cardsArray[i].scale = this.itemInfoGrp[i].scale;
            this.cardsArray[i].opacity = this.itemInfoGrp[i].opacity;
            this.cardsArray[i].position = this.itemInfoGrp[i].pos
            let lab = this.cardsArray[i].getChildByName('New Label').getComponent(cc.Label)
            lab.string = i
            // this.cardsArray[i].pos = initAttr['pos'];
        }
        this.touchListen()
        this.fivePageInfoInit()
        if (this.pageEndServer) {
            this.pageEndServer()
        }
    },

    nameInit() {
        for (let i = 0; i < this.cardsArray.length; i++) {
            if (this.cardsArray[i].getName() == '0') {
                this.cardsArray[i].zIndex = 0
            }
            if (this.cardsArray[i].getName() == '5') {
                this.cardsArray[i].zIndex = 1
            }
            if (this.cardsArray[i].getName() == '1') {
                this.cardsArray[i].zIndex = 2
            }
            if (this.cardsArray[i].getName() == '4') {
                this.cardsArray[i].zIndex = 3
            }
            if (this.cardsArray[i].getName() == '2') {
                this.cardsArray[i].zIndex = 4
            }
            if (this.cardsArray[i].getName() == '3') {
                this.cardsArray[i].zIndex = 5
            }
        }
    },
    //滑动监听
    touchListen() {
        this.touchNode.on(cc.Node.EventType.TOUCH_START, this.touchStartServer, this)
        this.touchNode.on(cc.Node.EventType.TOUCH_END, this.touchEndServer, this)
        this.touchNode.on(cc.Node.EventType.TOUCH_CANCEL, this.touchCancelServer, this)
    },
    //关闭监听
    closeTouchListen() {
        this.touchNode.targetOff(this)
        for (let i = 0; i < this.tweenGrp.length; i++) {
            if (this.tweenGrp[i]) {
                this.tweenGrp[i].stop()
                this.tweenGrp[i] = null
            }
        }
    },

    //触摸开始
    touchStartServer(event) {
        let location = event.getLocation();// 获取节点坐标
        this.firstX = location.x;
        this.firstY = location.y;
    },
    //触摸结束
    touchEndServer(event) {
        let touchPoint = event.getLocation();
        let endX = this.firstX - touchPoint.x;

        if (endX > 5) {
            //向左函数
            // console.log('left');
            this.revolve2Left()
        } else if (endX < -5) {
            //向右函数
            // console.log('right');
            this.revolve2Right()
        }
    },
    //取消触摸
    touchCancelServer(event) {
        let touchPoint = event.getLocation();
        let endX = this.firstX - touchPoint.x;
        let endY = this.firstY - touchPoint.y;

        if (endX > 0) {
            //向左函数
            // console.log('left');
            this.revolve2Left()
        } else {
            //向右函数
            // console.log('right');
            this.revolve2Right()
        }
    },

    //页面初始化
    fivePageInfoInit(index) {
        let inputIndex = this.pageIndex
        if (index >= 0) {
            inputIndex = index
        }
        this.pageInfoUpdateServer(0, this.pageGrp[inputIndex])
        if (inputIndex - 1 >= 0) {
            this.pageInfoUpdateServer(this.content.children.length - 1, this.pageGrp[inputIndex - 1])
        } else {
            this.pageInfoUpdateServer(this.content.children.length - 1, null)
        }
        if (inputIndex - 2 >= 0) {
            this.pageInfoUpdateServer(this.content.children.length - 2, this.pageGrp[inputIndex - 2])
        } else {
            this.pageInfoUpdateServer(this.content.children.length - 2, null)
        }
        if (inputIndex + 1 <= this.pageGrp.length - 1) {
            this.pageInfoUpdateServer(1, this.pageGrp[inputIndex + 1])
        } else {
            this.pageInfoUpdateServer(1, null)
        }
        if (inputIndex + 2 <= this.pageGrp.length - 1) {
            this.pageInfoUpdateServer(2, this.pageGrp[inputIndex + 2])
        } else {
            this.pageInfoUpdateServer(2, null)
        }
        // console.log(this.pageGrp[inputIndex - 2], this.pageGrp[inputIndex - 1], this.pageGrp[inputIndex], this.pageGrp[inputIndex + 1], this.pageGrp[inputIndex + 2])
    },



    //往左转一格 
    revolve2Right() {
        if (this.isMoveing) {
            return
        }
        // 往右旋转
        this.pageStartServer(false)

        if (this.pageIndex - 1 < 0) {
            return
        } else {
            this.pageIndex -= 1
        }
        // console.log(this.cardsArray)
        this.isMoveing = true

        for (let i = 0; i < this.cardsArray.length; i++) {
            // 所有节点编号+1
            if (this.cardsArray[i].num < this.cardsArray.length - 1)
                this.cardsArray[i].num += 1;
            else
                this.cardsArray[i].num = 0;
            // 获取目标属性
            let nextAttr = this.itemInfoGrp[this.cardsArray[i].num];
            // 改变z值
            this.cardsArray[i].zIndex = nextAttr.zIndex;
            this.tweenGrp[i] = cc.tween(this.cardsArray[i])
                .to(this.moveTime, { position: nextAttr.pos, scale: nextAttr.scale, opacity: nextAttr.opacity })
                .call(() => {
                    this.isMoveing = false
                    if (this.pageEndServer) {
                        this.pageEndServer()
                    }
                })
                .start()
        }
    },
    //往右转一格
    revolve2Left() {
        if (this.isMoveing) {
            return
        }
        // 往左旋转
        this.pageStartServer(true)
        //新增加的限制，未解锁到不能跳过去
        // if ((this.passLen / 10 + 1) < this.pageGrp[this.pageIndex + 1]) {
        //     gg.uiManager.addTip('未解锁该关卡等级，继续努力吧')
        //     return
        // }

        if (this.pageIndex + 1 > this.pageGrp.length - 1) {
            return
        } else {
            this.pageIndex += 1
        }

        this.isMoveing = true
        for (let i = 0; i < this.cardsArray.length; i++) {
            // 所有节点编号-1
            if (this.cardsArray[i].num > 0)
                this.cardsArray[i].num -= 1;
            else
                this.cardsArray[i].num = this.cardsArray.length - 1;
            // 获取目标属性
            let nextAttr = this.itemInfoGrp[this.cardsArray[i].num];
            // 改变z值
            this.cardsArray[i].zIndex = nextAttr.zIndex;
            this.tweenGrp[i] = cc.tween(this.cardsArray[i])
                .to(this.moveTime, { position: nextAttr.pos, scale: nextAttr.scale, opacity: nextAttr.opacity })
                .call(() => {
                    this.isMoveing = false
                    if (this.pageEndServer) {
                        this.pageEndServer()
                    }
                })
                .start()
        }
    },
    //动画取消
    cleartweenGrp() {
        if (this.tweenGrp) {
            for (let i = 0; i < this.tweenGrp.length; i++) {
                if (this.tweenGrp[i]) {
                    this.tweenGrp[i].stop()
                    this.tweenGrp[i] = null
                }
            }
        }
    },
});
